package experiment_01_Pram;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentManager {
	static List<Student> list = new ArrayList<Student>();
	private static Scanner scanner = new Scanner(System.in);

	public static void main(String[] args) {
		while (true) {
			System.out.println("**欢迎进入成绩管理系统**");
			System.out.println("1.增加");
			System.out.println("2.删除");
			System.out.println("3.查找");
			System.out.println("4.修改");
			System.out.println("5.查看");
			System.out.println("0.退出");
			System.out.println("请选择你想要进行的操作：");
			int choice = scanner.nextInt();
			if (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5 || choice == 0) {
				switch (choice) {
				case 1:
					add();
					break;
				case 2:
					delect();
					break;
				case 3:
					find();
					break;
				case 4:
					update();
					break;
				case 5:
					display();
					break;
				case 0:
					return;
				}
				System.out.println("\n");
			} else {
				System.out.println("输入不合法，请重新输入！");
			}
		}
	}

	private static void add() {
		String id, name;
		int age, Chinese, English, Math;
		boolean flag = true;
		System.out.println("请输入要增加的学生信息");
		System.out.print("学号：");
		id = scanner.next();
		System.out.print("姓名：");
		name = scanner.next();
		System.out.print("年龄：");
		age = scanner.nextInt();
		System.out.print("语文成绩：");
		Chinese = scanner.nextInt();
		System.out.print("数学成绩：");
		Math = scanner.nextInt();
		System.out.print("英语成绩：");
		English = scanner.nextInt();
		Student student = new Student(id, name, age, Chinese, English, Math);
		if (list.isEmpty()) {
			list.add(student);
			System.out.println("增加成功！");
		} else {
			for (Student st : list)
				if (st.getId().equals(id))
					flag = false;
			if (flag) {
				list.add(student);
				System.out.println("增加成功！");
			} else {
				System.out.println("该学生已经存在，增加失败！");
			}
		}
	}

	private static void delect() {
		boolean flag = true;
		System.out.print("请输入要删除的学生的学号：");
		String sID = scanner.next();
		if (list.isEmpty()) {
			System.out.println("学生信息为空，删除失败！");
		} else {
			for (int i = 0; i < list.size(); i++)
				if (list.get(i).getId().equals(sID)) {
					flag = false;
					list.remove(i);
				}
			if (!flag) {
				System.out.println("删除成功！");
			} else {
				System.out.println("不存在此学生，删除失败！");
			}
		}

	}

	private static void find() {
		System.out.print("请输入要查找的学生学号：");
		String sID = scanner.next();
		if (list.isEmpty()) {
			System.out.println("学生信息为空，查询失败！");
		} else {
			boolean flag = true;
			for (int i = 0; i < list.size(); i++)
				if (list.get(i).getId().equals(sID)) {
					System.out.println(list.get(i).toString());
					flag = false;
				}
			if (flag) {
				System.out.println("查询失败！");
			}

		}
	}

	private static void update() {
		System.out.print("请输入要修改的学生学号：");
		String sID = scanner.next();
		if (list.isEmpty()) {
			System.out.println("学生信息为空，修改失败！");
		} else {
			String id, name;
			int age, Chinese, English, Math;
			boolean flag = true;
			for (int i = 0; i < list.size(); i++)
				if (list.get(i).getId().equals(sID)) {
					System.out.print("姓名：");
					name = scanner.next();
					System.out.print("年龄：");
					age = scanner.nextInt();
					System.out.print("语文成绩：");
					Chinese = scanner.nextInt();
					System.out.print("英语成绩：");
					English = scanner.nextInt();
					System.out.print("数学成绩：");
					Math = scanner.nextInt();
					list.get(i).setName(name);
					list.get(i).setAge(age);
					list.get(i).setChinese(Chinese);
					list.get(i).setEnglish(English);
					list.get(i).setMath(Math);
					System.out.println(list.get(i).toString());
					flag = false;
				}
			if (!flag) {
				System.out.println("修改成功！");
			} else {
				System.out.println("该学生不存在，修改失败！");
			}
		}
	}

	private static void display() {
		if (list.isEmpty()) {
			System.out.println("学生信息为空，显示失败！");
		} else {
			for (Student st : list)
				System.out.println(st.toString());
			System.out.println("以上为所有学生信息，显示成功！");
		}

	}
}
